//@author A0081058H
package net.mysoc.w111j.ui.generatereport;

import java.util.Date;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import net.mysoc.w111j.service.ReportService;
import net.mysoc.w111j.ui.Constants;

/**
 * The model for reports. Is unused as charts currently receive information directly from the presenter.
 */
public class GenerateReportModel {
    private final ObjectProperty<Date> startDateProperty;
    private final StringProperty startDateStringProperty;

    /**
     * Default constructor.
     */
    public GenerateReportModel() {
        this.startDateProperty = new SimpleObjectProperty<>();
        this.startDateStringProperty = new SimpleStringProperty();

        // Sets date string when date property is changed.
        ChangeListener<Object> setDateString = new ChangeListener<Object>() {
            @Override
            public void changed(ObservableValue<?> o, Object oldVal, Object newVal) {
                try {
                    Date date = startDateProperty.get();
                    startDateStringProperty.set(Constants.DATE_FORMAT.format(date));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        startDateProperty.addListener(setDateString);
        startDateProperty.set(ReportService.getToday()); // Defaults to today.
    }

    public ObjectProperty<Date> startDateProperty() {
        return this.startDateProperty;
    }

    public void setStartDate(Date date) {
        this.startDateProperty.set(date);
    }

    public Date getStartDate() {
        return this.startDateProperty.get();
    }

    public StringProperty startDateStringProperty() {
        return this.startDateStringProperty;
    }

    public void setStartDateString(String date) {
        this.startDateStringProperty.set(date);
    }

    public String getStartDateString() {
        return this.startDateStringProperty.get();
    }
}
